import $kt from "@/uni_modules/uniapp-kantboot";
import shop from "@/js/shop"
import toolArea from "@/js/toolArea";

let send = async () => {
    return new Promise(async (resolve, reject) => {
        // 获取上一次加载的语言
        let lastLoadingLanguage = this.$kt.storage.get("lastLoadingLanguage");
        // 如果上一次加载的语言和当前语言一致
        if (lastLoadingLanguage === this.$i18n.getLanguageCode()) {
            // 获取上一次加载时间
            let lastLoadingTime = this.$kt.storage.get("lastLoadingTime");
            // 如果小于两小时，则不加载
            if (lastLoadingTime !== null && new Date().getTime() - lastLoadingTime < 2 * 60 * 60 * 1000) {
                this.isInitFinally = true;
                return;
            }
        }

        // 清除所有requestNumberBy开头的缓存
        let keys = Object.keys(localStorage);
        for (let i = 0; i < keys.length; i++) {
            if (keys[i].indexOf("requestNumberBy") === 0) {
                localStorage.removeItem(keys[i]);
            }
        }

        this.loadingNum = this.$kt.storage.get("loadingNum");
        if (this.loadingNum === null) {
            this.loadingNum = 1;
        } else {
            this.loadingNum = parseInt(this.loadingNum);
        }
        let loadingNum = this.loadingNum + 1;
        if (loadingNum > this.maxLoadingNum) {
            loadingNum = 1;
        }
        this.$kt.storage.set("loadingNum", loadingNum);

        await this.$i18n.loadLanguage("appFront", "en");
        await this.$i18n.loadLanguage("appFront");

        setTimeout(() => {
            this.isInitFinally = true;
        }, 1000);

        // 设置存储时间
        this.$kt.storage.set("lastLoadingTime", new Date().getTime());
        // 设置最后一次加载的语言
        this.$kt.storage.set("lastLoadingLanguage", this.$i18n.getLanguageCode());
        resolve();
    });

}

export default {
    send
}